package cn.edu.cuit.mockfns.fam.tuples;

import cn.edu.cuit.mockfns.fam.Condition;
import cn.edu.cuit.mockfns.fam.FAMEngine;

public class Function
{

    private State currentState;
    private Event<?> event;
    private State nextState;
    private Condition[] conditions;
    private FAMEngine famEngine;

    public Function()
    {

    }

    public Function(FAMEngine famEngine, State currentState, Event<?> event, State nextState)
    {
        this.famEngine = famEngine;
        this.currentState = currentState;
        this.event = event;
        this.nextState = nextState;
    }

    public Function(FAMEngine famEngine, State currentState, Event<?> event, State nextState, Condition... conditions)
    {
        this.famEngine = famEngine;
        this.currentState = currentState;
        this.event = event;
        this.nextState = nextState;
        this.conditions = conditions;
    }

    public boolean accept( State currentState, Event<?> event )
    {

        if( conditions == null )
        {
            return this.currentState == currentState && this.event == event;
        }

        for( Condition condition : conditions )
        {
            if( condition == null )
            {
                continue;
            }

            if( !condition.evaluate( currentState.famEngineInstance(), event ) )
            {
                return false;
            }

        }

        return true;

    }

    public void setFamengine( FAMEngine famEngine )
    {
        this.famEngine = famEngine;
    }

    public FAMEngine getFamengine()
    {
        return famEngine;
    }
}
